﻿using System;
using System.Collections.Generic;
using System.Linq;
using AMM.CustomClassLibrary;
using AMM.DataAccess.Store;
using AMM.Model;
using AMM.Model.Store;

namespace AMM.Business.Store {
    public class ProductInStoreOrderProcessor : BaseProcessor {

        private int _pageSize;
        private ProductInStoreOrderRepository _productInStoreOrderRepository;

        public ProductInStoreOrderProcessor(int pageSize = int.MaxValue) {
            this._pageSize = pageSize;
            this._productInStoreOrderRepository = new ProductInStoreOrderRepository(pageSize);
        }

        public ProductInStoreOrder FetchProductInStoreOrderById(int productInStoreOrderId) {
            try {
                return this._productInStoreOrderRepository.FetchProductInStoreOrderById(productInStoreOrderId);
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("获取指定成品入库单失败！");
            }
        }

        public List<ProductInStoreOrderViewModel> FetchProductInStoreOrderViewModelList(ProductInStoreOrderSearchModel searchModel, int pageIndex = 1) {
            try {
                List<ProductInStoreOrder> resultList = this._productInStoreOrderRepository.FetchProductInStoreOrderList(searchModel, pageIndex);
                return resultList.Select(x => {
                    return new ProductInStoreOrderViewModel() {
                        Key = x.ProductInStoreOrderId,
                        Code = x.Code,
                        ProductCode = x.Product.Code,
                        ProductName = x.Product.Name,
                        ProductInStoreType = x.Basic.Code,
                        StoreBasisCode = x.StoreBasi.Code,
                        ProduceConductCode = x.ProduceConduct.Code,
                        IsOutsource = x.IsOutsource,
                        ProduceUnitCode = x.IsOutsource
                            ? (x.VendorId.HasValue ? x.Vendor.Code : string.Empty)
                            : (x.ProduceUnitId.HasValue ? x.Basic1.Code : string.Empty),
                        Quantity = x.Quantity,
                        ReceiptCode = x.ReceiptCode,
                        OrderStatus = MethodHelper.GetEnumsDescription(typeof(AuditStatus), x.Status),
                        AuditedBy = x.AuditedBy,
                        AuditedDatetime = x.AuditedDatetime,
                        Remarks = x.Remarks,
                        CreatedBy = x.CreatedBy,
                        CreatedDatetime = x.CreatedDatetime
                        //,ModifiedBy = x.ModifiedBy,
                        //ModifiedDatetime = x.ModifiedDatetime
                    };
                }).ToList();
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("获取成品入库单列表失败！");
            }
        }

        public void Insert(ProductInStoreOrder productInStoreOrder) {
            try {
                productInStoreOrder.ProductInStoreOrderId = Guid.NewGuid().GetHashCode();
                productInStoreOrder.CreatedDatetime = DateTime.Now;
                productInStoreOrder.Status = (int)AuditStatus.Waiting;
                this._productInStoreOrderRepository.Insert(productInStoreOrder);
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("插入新成品入库单失败！");
            }
        }

        public void Update(ProductInStoreOrder productInStoreOrder) {
            try {
                ProductInStoreOrder oldProductInStoreOrder = this.FetchProductInStoreOrderById(productInStoreOrder.ProductInStoreOrderId);
                if (!oldProductInStoreOrder.RowVersion.SequenceEqual(productInStoreOrder.RowVersion)) {
                    throw this.BuildException("更新成品入库单失败，数据不一致性错误！");
                }
                productInStoreOrder.ModifiedDatetime = DateTime.Now;
                this._productInStoreOrderRepository.UpdateProductInStoreOrder(productInStoreOrder);
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("更新成品入库单失败！");
            }
        }

        public void Audit(ProductInStoreOrder productInStoreOrder) {
            try {
                ProductInStoreOrder oldProductInStoreOrder = this.FetchProductInStoreOrderById(productInStoreOrder.ProductInStoreOrderId);
                if (!oldProductInStoreOrder.RowVersion.SequenceEqual(productInStoreOrder.RowVersion)) {
                    throw this.BuildException("审核成品入库单失败，数据不一致性错误！");
                }
                productInStoreOrder.AuditedDatetime = DateTime.Now;
                this._productInStoreOrderRepository.UpdateProductInStoreOrder(productInStoreOrder);
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("审核成品入库单失败！");
            }
        }

        public void Delete(ProductInStoreOrder productInStoreOrder) {
            try {
                this._productInStoreOrderRepository.Delete(productInStoreOrder);
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("删除成品入库单失败！");
            }
        }

        public void Delete(params int[] productInStoreOrderIds) {
            try {
                this._productInStoreOrderRepository.DeleteMultipleProductInStoreOrder(productInStoreOrderIds);
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("删除成品入库单失败！");
            }
        }

        public int GetTheMaxFlowNumber(string startString) {
            try {
                return this._productInStoreOrderRepository.GetTheMaxFlowNumber(startString) + 1;
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("获取成品入库单最大流水号失败！");
            }
        }
    }
}
